package com.wish.finance.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.finance.entity.CwDealPerDay;
import com.wish.finance.entity.CwDealPerDayDetailReceiptDto;
import com.wish.finance.entity.vo.CwDealPaySum;
import com.wish.finance.entity.vo.CwDealPerDayVo;
import com.wish.finance.service.ICwDealPerDayService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/cw-deal-per-day")
public class CwDealPerDayController {


    @Autowired
    ICwDealPerDayService cwDealPerDayService;
    //列表
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        if(cwDealPerDayVo.getStarttime()==null){
            //开始时间
            //当前日期字符串，格式：yyyy-MM-dd
            String today= DateUtil.today();
            String[] split = today.split("-");
            String str = split[0]+"-"+split[1]+"-"+"01";
            Date date2 = DateUtil.parse(str, "yyyy-MM-dd");
            cwDealPerDayVo.setStarttime(date2);
        }
        if(cwDealPerDayVo.getEndtime()==null){
            //获得当前时间
            DateTime date = DateUtil.date();
            //结束时间
            DateTime newDate2 = DateUtil.offsetDay(date, -1);
            cwDealPerDayVo.setEndtime(newDate2);
        }
        if(cwDealPerDayVo.getStarttime()!=null){
            Date starttime = cwDealPerDayVo.getStarttime();
            DateTime starttime1 = DateUtil.offsetDay(starttime, -1);
            cwDealPerDayVo.setStarttime(starttime1);
        }
        if(cwDealPerDayVo.getEndtime()!=null){
            Date endtime = cwDealPerDayVo.getEndtime();
            DateTime endtime1 = DateUtil.offsetDay(endtime, -1);
            cwDealPerDayVo.setEndtime(endtime1);
        }
        Page<CwDealPerDay> page = new Page<>(cwDealPerDayVo.getPageNum(), cwDealPerDayVo.getSize());
        IPage<CwDealPerDay> page1 = cwDealPerDayService.listShow(page,cwDealPerDayVo);
        return  AjaxResult.success(page1);
    }
    //列表数据
    @PostMapping("sumList")
    public AjaxResult sumList(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        if(cwDealPerDayVo.getStarttime()==null){
            //当前日期字符串，格式：yyyy-MM-dd
            String today= DateUtil.today();
            String[] split = today.split("-");
            String str = split[0]+"-"+split[1]+"-"+"01";
            Date date2 = DateUtil.parse(str, "yyyy-MM-dd");
            cwDealPerDayVo.setStarttime(date2);
        }
        if(cwDealPerDayVo.getEndtime()==null){
            //获得当前时间
            DateTime date = DateUtil.date();
            //结束时间
            DateTime newDate2 = DateUtil.offsetDay(date, -1);
            cwDealPerDayVo.setEndtime(newDate2);
        }
        CwDealPerDayVo c1 = cwDealPerDayService.sumList(cwDealPerDayVo);
        if(c1==null){
            BigDecimal bigDecimal = new BigDecimal(0);
            //总金额
            c1.setTotal(bigDecimal);
            //押金
            c1.setDeposit(bigDecimal);
            //租金
            c1.setRent(bigDecimal);
            //电费
            c1.setElectricityFee(bigDecimal);
            //水费
            c1.setWaterFee(bigDecimal);
            //宽带
            c1.setNetFee(bigDecimal);
            //燃气费
            c1.setGasFee(bigDecimal);
            //有线电视
            c1.setCableFee(bigDecimal);
            //其他
            c1.setOtherFee(bigDecimal);
            return AjaxResult.success(c1);
        }
        return AjaxResult.success(c1);
    }


    //收入列表查看
    @PostMapping("shourumingxi")
    public AjaxResult shourumingxi(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        Date date = cwDealPerDayVo.getStarttime();
        String format = DateUtil.format(date, "yyyy-MM-dd");
        Date date1 = DateUtil.parse(format);
        cwDealPerDayVo.setDealDate(date1);
        Page<CwDealPerDayDetailReceiptDto> page = new Page<>(cwDealPerDayVo.getPageNum(), cwDealPerDayVo.getSize());
        IPage<CwDealPerDayDetailReceiptDto> pg = null;
        //应付(支出)
        if(cwDealPerDayVo.getDealtype()==1){
            pg = cwDealPerDayService.shouRuMingXiZhiChu(page,cwDealPerDayVo);
        }
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            pg = cwDealPerDayService.shourumingxi(page,cwDealPerDayVo);
        }
        System.out.println(pg.getRecords());
        return AjaxResult.success(pg);
    }

    //列表查看数据统计
    @PostMapping("chaKanXinXiTongJi")
    public AjaxResult chaKanXinXiTongJi(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        //应付(支出)
        CwDealPaySum cwDealPaySum = null;
       /* if(cwDealPerDayVo.getDealtype()==1){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }*/
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }
        Date dealDate = cwDealPerDayVo.getDealDate();
        cwDealPaySum.setDealDate(dealDate);
        cwDealPaySum.setDealtype(cwDealPerDayVo.getDealtype());
        System.out.println(cwDealPaySum+"===========");
        return AjaxResult.success(cwDealPaySum);
    }

    //上一天
    @PostMapping("tosday")
    public AjaxResult tosday(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        Date dealDate = cwDealPerDayVo.getDealDate();
        DateTime newDate2 = DateUtil.offsetDay(dealDate, -1);
        cwDealPerDayVo.setDealDate(newDate2);
        Page<CwDealPerDayDetailReceiptDto> page = new Page<>(cwDealPerDayVo.getPageNum(), cwDealPerDayVo.getSize());
        IPage<CwDealPerDayDetailReceiptDto> pg = null;
        //应付(支出)
        if(cwDealPerDayVo.getDealtype()==1){
            pg = cwDealPerDayService.shouRuMingXiZhiChu(page,cwDealPerDayVo);
        }
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            pg = cwDealPerDayService.shourumingxi(page,cwDealPerDayVo);
        }
        System.out.println(pg.getRecords()+"================");
        return AjaxResult.success(pg);
    }

    //上一天收入列表查看
    @PostMapping("chaKanXinXiTongJi1")
    public AjaxResult chaKanXinXiTongJi1(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        //应付(支出)
        CwDealPaySum cwDealPaySum = null;
        Date dealDate = cwDealPerDayVo.getDealDate();
        DateTime newDate2 = DateUtil.offsetDay(dealDate, -1);
        cwDealPerDayVo.setDealDate(newDate2);
       /* if(cwDealPerDayVo.getDealtype()==1){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }*/
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }
        Date dealDate3 = cwDealPerDayVo.getDealDate();
        cwDealPaySum.setDealDate(dealDate3);
        cwDealPaySum.setDealtype(cwDealPerDayVo.getDealtype());
        System.out.println(cwDealPaySum+"===========");
        return AjaxResult.success(cwDealPaySum);
    }


    //下一天
    @PostMapping("ttday")
    public AjaxResult ttday(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        Date dealDate = cwDealPerDayVo.getDealDate();
        DateTime newDate2 = DateUtil.offsetDay(dealDate, +1);
        cwDealPerDayVo.setDealDate(newDate2);
        Page<CwDealPerDayDetailReceiptDto> page = new Page<>(cwDealPerDayVo.getPageNum(), cwDealPerDayVo.getSize());
        IPage<CwDealPerDayDetailReceiptDto> pg = null;
        //应付(支出)
        if(cwDealPerDayVo.getDealtype()==1){
            pg = cwDealPerDayService.shouRuMingXiZhiChu(page,cwDealPerDayVo);
        }
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            pg = cwDealPerDayService.shourumingxi(page,cwDealPerDayVo);
        }
        System.out.println(pg.getRecords()+"================");
        return AjaxResult.success(pg);
    }

    //下一天收入列表查看
    @PostMapping("chaKanXinXiTongJi2")
    public AjaxResult chaKanXinXiTongJi2(@RequestBody CwDealPerDayVo cwDealPerDayVo){
        //应付(支出)
        CwDealPaySum cwDealPaySum = null;
        Date dealDate = cwDealPerDayVo.getDealDate();
        DateTime newDate2 = DateUtil.offsetDay(dealDate, +1);
        cwDealPerDayVo.setDealDate(newDate2);
       /* if(cwDealPerDayVo.getDealtype()==1){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }*/
        //应收(收入)
        if(cwDealPerDayVo.getDealtype()==2){
            cwDealPaySum = cwDealPerDayService.chaKanXinXiTongJi1(cwDealPerDayVo);
        }
        Date dealDate3 = cwDealPerDayVo.getDealDate();
        cwDealPaySum.setDealDate(dealDate3);
        cwDealPaySum.setDealtype(cwDealPerDayVo.getDealtype());
        System.out.println(cwDealPaySum+"===========");
        return AjaxResult.success(cwDealPaySum);
    }
}
